package com.overlord.im.connector.config;

import com.google.common.collect.Maps;
import com.overlord.im.commons.message.MessageProtocolConverter;
import com.overlord.im.commons.message.OverlordProtocol;
import com.overlord.im.commons.message.Protocol;
import com.overlord.im.commons.message.protobuf.ProtobufMessageConverter;
import com.overlord.im.commons.mq.MqConstants;
import com.overlord.im.commons.mq.QueueUtils;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Map;

/**
 * @ClassName OverlordConfig
 * @Author wuwei
 * @Description NONE
 * @Date 2020/5/29 15:59
 **/
@Configuration
public class OverlordConfig {

    @Value("${im.server.host}")
    private String host;

    @Value("${im.server.port}")
    private int port;

    @Value("${im.message.ttl}")
    private int ttl;

    @Bean
    public MessageProtocolConverter messageProtocolConverter() {
        return new ProtobufMessageConverter();
    }

    @Bean
    public Protocol protocol(MessageProtocolConverter converter) {
        return new OverlordProtocol(converter);
    }

    @Bean
    public Exchange imExchange() {
        return new DirectExchange(MqConstants.OVERLORD_IM_EXCHANGE, true, false);
    }

    @Bean
    public Exchange overTimeExchange() {
        return new DirectExchange(MqConstants.OVERLORD_IM_MESSAGE_OVER_TIME_EXCHANGE, true, false);
    }

    @Bean
    public Queue redirectMsgQueue() {
        Map<String, Object> args = Maps.newHashMap();
        args.put("x-dead-letter-exchange", MqConstants.OVERLORD_IM_EXCHANGE);
        args.put("x-dead-letter-routing-key", MqConstants.OVERLORD_IM_MESSAGE_OVER_TIME_QUEUE);
        args.put("x-message-ttl", ttl);
        return new Queue(MqConstants.OVERLORD_IM_MESSAGE_DO_REDIRECT + host + ":" + port, true, false, false, args);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(redirectMsgQueue()).to(overTimeExchange())
            .with(MqConstants.OVERLORD_IM_MESSAGE_DO_REDIRECT + host + ":" + port).noargs();
    }

    @Bean
    public Queue checkStatusQueue() {
        return QueueUtils.createDefaultQueue(MqConstants.OVERLORD_IM_ACK_MESSAGE_QUEUE);
    }

    @Bean
    public Binding checkStatusBinding() {
        return BindingBuilder.bind(checkStatusQueue()).to(imExchange())
            .with(MqConstants.OVERLORD_IM_ACK_MESSAGE_QUEUE).noargs();
    }

    @Bean
    public Queue overTimeQueue() {
        return QueueUtils.createDefaultQueue(MqConstants.OVERLORD_IM_MESSAGE_OVER_TIME_QUEUE);
    }

    @Bean
    public Binding overTimeBinding() {
        return BindingBuilder.bind(overTimeQueue()).to(imExchange())
            .with(MqConstants.OVERLORD_IM_MESSAGE_OVER_TIME_QUEUE).noargs();
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return stringRedisTemplate;
    }
}
